#!/bin/bash
# 修复后的测试脚本

echo "=== 重新编译加密解密系统 ==="
make clean
make

if [ $? -ne 0 ]; then
    echo "编译失败！"
    exit 1
fi

echo "编译成功！"

# 检查原有文件
if [ -f "stream_chn0.h265" ] && [ -f "stream_chn0.h265.enc" ] && [ -f "stream_chn0.h265.key" ]; then
    echo ""
    echo "发现之前的加密文件，现在尝试解密..."
    echo ""
    
    # 使用修复后的解密程序
    echo "运行解密程序..."
    echo "stream_chn0.h265.enc
stream_chn0.h265.test_decrypt
stream_chn0.h265.key
1
10086" | ./bin/decrypt
    
    if [ $? -eq 0 ] && [ -f "stream_chn0.h265.test_decrypt" ]; then
        echo ""
        echo "解密成功！"
        echo "验证文件..."
        
        # 比较文件大小
        original_size=$(wc -c < stream_chn0.h265)
        decrypted_size=$(wc -c < stream_chn0.h265.test_decrypt)
        
        echo "原始文件大小: $original_size 字节"
        echo "解密文件大小: $decrypted_size 字节"
        
        if [ "$original_size" -eq "$decrypted_size" ]; then
            echo "✓ 文件大小匹配"
            
            # 比较文件内容
            if cmp -s stream_chn0.h265 stream_chn0.h265.test_decrypt; then
                echo "✓ 文件内容完全一致！"
                echo ""
                echo "=== 解密成功！==="
                echo "原始文件: stream_chn0.h265"
                echo "加密文件: stream_chn0.h265.enc"
                echo "解密文件: stream_chn0.h265.test_decrypt"
                echo "密钥文件: stream_chn0.h265.key"
            else
                echo "✗ 文件内容不一致"
                echo "前10字节对比:"
                xxd -l 10 stream_chn0.h265 | head -1
                xxd -l 10 stream_chn0.h265.test_decrypt | head -1
            fi
        else
            echo "✗ 文件大小不匹配"
        fi
    else
        echo "解密失败！"
    fi
else
    echo "未找到之前的加密文件，请先运行加密程序创建测试文件。"
fi